package com.zmn.plat.business.impl.sendtemplate;

import java.util.Objects;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.utils.MQPushUtil;
import com.zmn.plat.business.interfaces.sendtemplate.SendSmsInfoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.constant.ZmnSmsConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.push.ZmnSms;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：发送微信模板消息
 *
 * @author wangjie
 * @since 2020/6/19 10:47
 **/
@Slf4j
public class WeChatSendSmsInfoBServiceImpl implements SendSmsInfoBService {

    private static final String TAG = "发送微信模板消息";

    private volatile static WeChatSendSmsInfoBServiceImpl weChatSendSmsInfoBService;

    private WeChatSendSmsInfoBServiceImpl() {}

    @Override
    public Integer doSend(MsgTemplate msgTemplate, ZmnSms zmnSms) {

        log.info("[{}],发送微信模板消息参数。msgTemplate:{},zmnSms:{}", TAG, JSONObject.toJSONString(msgTemplate), JSONObject.toJSONString(zmnSms));
        // 判断微信发送必要条件
        if (StringUtil.isBlank(msgTemplate.getWechatMessage())) {
            log.info("[{}],发送失败,微信内容为null。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_CONTENT, ZmnSmsConsts.MSG_SEND_WEICHART_TERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_CONTENT);
        }
        if (Objects.isNull(zmnSms.getOpenId())) {
            log.info("[{}],发送失败。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_OPENID, ZmnSmsConsts.MSG_SEND_NO_OPENID_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_OPENID);
        }
        if (Objects.isNull(msgTemplate.getWechatTempId())) {
            log.info("[{}],发送失败。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_WECHATTEMPID, ZmnSmsConsts.MSG_NO_WEICHART_TEMPLATE_ID_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_WECHATTEMPID);
        }
        if (StringUtil.isBlank(zmnSms.getToken())) {
            log.info("[{}],发送失败。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_TOCKEN, ZmnSmsConsts.MSG_SEND_WECHAT_NO_TOKEN_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_TOCKEN);
        }
        if (StringUtil.isBlank(zmnSms.getWeChatAppId())) {
            log.info("[{}],发送失败。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_WECHATAPPID, ZmnSmsConsts.MSG_SEND_WECHAT_NO_APP_ID_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_NO_WECHATAPPID);
        }

        ResultDTO result;
        if (Objects.nonNull(zmnSms.getPagePath()) && Objects.nonNull(zmnSms.getWeChatAppId())) {

            result = MQPushUtil.sendWeChatTemplateInfoAndJumpMiniProgramByToken(zmnSms.getOpenId(), msgTemplate.getWechatTempId(), zmnSms.getAppletsAppId(), zmnSms.getPagePath(),
                zmnSms.getWeChartParams(), zmnSms.getToken());
            log.info("[{}],拥有跳转到的小程序能力.微信参数params：{},pagePath:{},openId:{},wechatTempId:{},appId:{}", TAG, zmnSms.getWeChartParams(), zmnSms.getPagePath(), zmnSms.getOpenId(),
                msgTemplate.getWechatTempId(), zmnSms.getAppletsAppId());
        } else if (Objects.nonNull(zmnSms.getUrl())) {

            result = MQPushUtil.sendWeChatTemplateInfoAndJumpUrlByToken(zmnSms.getOpenId(), msgTemplate.getWechatTempId(), zmnSms.getUrl(), zmnSms.getWeChartParams(), zmnSms.getToken());
            log.info("[{}],拥有模板跳转链接能力.微信参数params：{},url:{},openId:{},wechatTempId:{}", TAG, zmnSms.getWeChartParams(), zmnSms.getUrl(), zmnSms.getOpenId(), msgTemplate.getWechatTempId());
        } else {

            result = MQPushUtil.sendWeChatTemplateInfoByToken(zmnSms.getOpenId(), msgTemplate.getWechatTempId(), zmnSms.getWeChartParams(), zmnSms.getToken());
            log.info("[{}],普通模板消息.微信参数params：{},openId:{},wechatTempId:{}}", TAG, zmnSms.getWeChartParams(), zmnSms.getOpenId(), msgTemplate.getWechatTempId());
        }

        if (result.getStatusBool()) {
            log.info("[{}],发送成功。code:{},msg:{}", TAG, ZmnSmsConsts.MSG_SEND_SUCCESS, ZmnSmsConsts.MSG_SEND_SUCCESS_STR);
            return ZmnSmsConsts.MSG_SEND_SUCCESS;
        } else {
            log.error("[{}],发送失败。code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_ERROR, result.getMessage());
            // return ZmnSmsConsts.MSG_SEND_WEICHART_TERROR;
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_WECHAT_ERROR);
        }
    }

    /**
     * 描述：获取该类的单例对象
     *
     * @return com.zmn.plat.business.impl.sendtemplate.ZmnWeChatSendSmsInfoBServiceImpl
     * @author wangjie
     * @since 2020/6/19 10:49
     **/
    public static WeChatSendSmsInfoBServiceImpl getSingleton() {
        if (Objects.isNull(weChatSendSmsInfoBService)) {
            synchronized (WeChatSendSmsInfoBServiceImpl.class) {
                if (Objects.isNull(weChatSendSmsInfoBService)) {
                    weChatSendSmsInfoBService = new WeChatSendSmsInfoBServiceImpl();
                }
            }
        }
        return weChatSendSmsInfoBService;
    }
}
